/**
 * Copyright (c) 2007,中企动力华南研发中心
 * All rights reserved.
 */
package com.ce.pms.house.dao.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.hibernate.Query;
import com.ce.pms.base.dao.impl.PmsBaseDaoImpl;
import com.ce.pms.common.util.StringAnalysiUtil;
import com.ce.pms.house.dao.ICoWorkerDao;
import com.ce.pms.house.model.CoWorkerDomain;
import com.ce.pms.house.vo.CoWorkerVO;
import com.sitechasia.webx.core.support.Page;

/**
 * 功能描述：协作伙伴持久化层
 * @author 罗军林 创建日期：2008-6-30
 */
public class CoWorkerDaoImpl extends PmsBaseDaoImpl<CoWorkerDomain> implements ICoWorkerDao{

	/*
	 * (non-Javadoc)
	 * @see com.ce.pms.house.dao.ICoWorkerDao#queryCoWorker(com.ce.pms.house.vo.CoWorkerVO, int, int)
	 */
	public Page queryCoWorker(CoWorkerVO queryCondition, int pageNo, int pageSize) {
		Map<String,Object> parameter=new HashMap<String, Object>();

		StringBuffer hql=new StringBuffer();
		hql.append("FROM OrganizationDO o,CoWorkerDomain c WHERE c.participant = o and ")
		.append(buildQueryCondition(queryCondition,parameter))
		.append(" order by c.sysInfo.sysDate desc");

		return this.pagedNameQueryByPageNo(hql.toString(), pageNo, pageSize, parameter);
	}

	/*
	 * (non-Javadoc)
	 * @see com.ce.pms.house.dao.ICoWorkerDao#queryAllCoWorker(com.ce.pms.house.vo.CoWorkerVO)
	 */
	public List queryAllCoWorkerByCoWorkerType(CoWorkerVO queryCondition) {

		StringBuffer hql=new StringBuffer();
		hql.append("FROM OrganizationDO o,CoWorkerDomain c WHERE c.participant = o  ")
        	.append(" and c.coworkerType.code=? and c.coworkerType.value=? ")
        	.append(buildQueryIDS(queryCondition))
        	.append(" order by c.sysInfo.sysDate desc");

		Object[] parameters=new Object[]{queryCondition.getCoworkerTypeCode(),
										queryCondition.getCoworkerTypeValue(),
										};
		Query query=this.createQuery(hql.toString(), parameters);
		return query.list();
	}

	/*
	 * (non-Javadoc)
	 * @see com.ce.pms.house.dao.ICoWorkerDao#queryCoWorkerByName(com.ce.pms.house.vo.CoWorkerVO)
	 */
	public Long countCoWorkerByName(CoWorkerVO queryCondition,String oldName){
		Map<String,Object> parameter=new HashMap<String, Object>();

		StringBuffer hql=new StringBuffer();
		hql.append("select count(*) FROM OrganizationDO o,CoWorkerDomain c WHERE c.participant = o and ")
        .append(" c.coworkerType.code=:code and c.coworkerType.value=:value ");

		parameter.put("code", queryCondition.getCoworkerTypeCode());
		parameter.put("value", queryCondition.getCoworkerTypeValue());

		if(null!=queryCondition.getName() && queryCondition.getName().trim().length()>0){
			hql.append(" and o.participantName=:participantName");
			parameter.put("participantName", queryCondition.getName());
		}
		if(oldName!=null){
			hql.append(" and o.participantName <> :oldParticipantName");
			parameter.put("oldParticipantName", oldName);
		}

		Query query=this.createNameQuery(hql.toString(), parameter);

		Object result=query.uniqueResult();
		return (result != null ? (Long)result : null);

	}


	/*
	 * (non-Javadoc)
	 * @see com.ce.pms.house.dao.ICoWorkerDao#countCoWorkerByParticipantId(com.ce.pms.house.vo.CoWorkerVO)
	 */
	public Long countCoWorkerByParticipantId(CoWorkerVO queryCondition){
		StringBuffer hql=new StringBuffer();
		hql.append("select count(*) FROM CoWorkerDomain c ")
		.append("WHERE c.participant.participantId = ?").append(queryCondition.getParticipantId()).append(" AND　")
        .append("c.coworkerType.code=? and c.coworkerType.value=?");

		Object[] parameters=new Object[]{queryCondition.getCoworkerTypeCode(),
				queryCondition.getCoworkerTypeValue(),
				};

		Query query=this.createQuery(hql.toString(), parameters);
		Object result=query.uniqueResult();

		return (result != null ? (Long)result : null);

	}

	/** 功能描述:构建查询条件
	 * @param queryCondition
	 * @return
	 * Create author:罗军林
	 * Create on:2008-7-1
	 * Edit author:
	 * Edit on:
	 * Why:
	 */
	private StringBuffer buildQueryCondition(CoWorkerVO queryCondition,final Map<String,Object> parameter) {
		StringBuffer condition=new StringBuffer(" 1=1 ");
		if( null!=queryCondition.getName() && queryCondition.getName().trim().length()>0){
			condition.append( " and o.participantName like :participantName");
			parameter.put("participantName", "%"+queryCondition.getName()+"%");
		}
		if( null!=queryCondition.getContactor() && queryCondition.getContactor().trim().length()>0){
			condition.append( " and o.contactor like :contactor");
			parameter.put("contactor", "%"+queryCondition.getContactor()+"%");
		}
		if(null!=queryCondition.getCoworkerTypeCode()){
			condition.append(" and c.coworkerType.code=:coworkerTypeCode ");
			parameter.put("coworkerTypeCode", queryCondition.getCoworkerTypeCode());
		}
		if(null!=queryCondition.getCoworkerTypeValue()){
			condition.append(" and c.coworkerType.value=:coworkerTypeValue ");
			parameter.put("coworkerTypeValue", queryCondition.getCoworkerTypeValue());
		}
		condition.append(buildQueryIDS(queryCondition));
		return condition;
	}

	/** 功能描述:构建ID列表查询条件<br>
	 * @param queryCondition
	 * @param condition<br>
	 * Create author:罗军林<br>
	 * Create on:2008-7-17<br>
	 * Edit author:<br>
	 * Edit on:<br>
	 * Why:<br>
	 */
	private StringBuffer buildQueryIDS(CoWorkerVO queryCondition) {
		StringBuffer condition=new StringBuffer("");
		if( null != queryCondition.getCoWorkerIDs() && queryCondition.getCoWorkerIDs().length()>0){
			condition.append(" and c.participantRoleId ")
					.append(queryCondition.getIsIn() ? "" : " not ")
					.append(" in (").append(queryCondition.getCoWorkerIDs()).append(") ");
		}
		return condition;
	}

}
